package com.mikufufu.blog.controller.admin;

import com.mikufufu.blog.common.annotation.OperationLog;
import com.mikufufu.blog.common.constant.Constant;
import com.mikufufu.blog.common.enums.ModuleType;
import com.mikufufu.blog.common.enums.OperationType;
import com.mikufufu.blog.model.dto.admin.ArticleDTO;
import com.mikufufu.blog.model.dto.admin.SearchArticleParam;
import com.mikufufu.blog.model.entity.*;
import com.mikufufu.blog.model.vo.admin.ArticleVO;
import com.mikufufu.blog.service.*;
import com.mikufufu.blog.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mikufufu.blog.common.entity.AjaxResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiImplicitParam;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@RestController
@RequestMapping("/admin/article")
@Api(tags = "文章接口")
public class ArticleAdminController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CollectionService collectionService;

    @Autowired
    private CollectionArticleService collectionArticleService;

    @Autowired
    private ArticleTagService articleTagService;

    @Autowired
    private SysSettingService sysSettingService;

    @GetMapping("/detail")
    @ApiOperation(value = "获取文章详情")
    @ApiImplicitParam(name = "id", value = "文章id", required = true)
    public AjaxResult<ArticleVO> detail(Integer id) {
        return AjaxResult.data(articleService.getArticleById(id));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改文章")
    public AjaxResult<Boolean> update(@RequestBody @Validated ArticleDTO article) {
        if (StringUtils.isEmpty(article.getId())){
            return AjaxResult.error("文章id不能为空");
        }
        articleService.updateArticle(article);
        return AjaxResult.success();
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增文章")
    public AjaxResult<Boolean> add(@RequestBody @Validated ArticleDTO article) {
        return AjaxResult.status(articleService.addArticle(article));
    }

    @PostMapping("/addCollection")
    @ApiOperation(value = "新建合集")
    @OperationLog(module = ModuleType.ARTICLE, type = OperationType.INSERT)
    public AjaxResult<Boolean> addCollection(@RequestBody Collection collection) {
        return AjaxResult.status(collectionService.save(collection));
    }

    @DeleteMapping("/deleteCollection")
    @ApiOperation(value = "删除合集")
    @OperationLog(module = ModuleType.ARTICLE, type = OperationType.DELETE)
    public AjaxResult<Boolean> deleteCollection(Integer id,Integer force) {
        LambdaQueryWrapper<CollectionArticle> queryWrapper = new LambdaQueryWrapper<CollectionArticle>().eq(CollectionArticle::getCollectionId, id);
        // 如果合集下有文章且未选着强制删除合集，则不允许删除
        if (Constant.NO.equals(force) && collectionArticleService.count(queryWrapper) > 0) {
            return AjaxResult.status(false);
        }
        collectionArticleService.remove(queryWrapper);
        return AjaxResult.status(collectionService.removeById(id));
    }

    // 收入进合集
    @GetMapping("/addArticleToCollection")
    @ApiOperation(value = "给指定集合新增文章" , notes = "给指定集合新增文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "collectionId", value = "合集id", required = true),
            @ApiImplicitParam(name = "articleId", value = "文章id", required = true)
    })
    public AjaxResult<Boolean> addArticleToCollection(Integer collectionId, Integer articleId) {
        CollectionArticle collectionArticle = new CollectionArticle();
        collectionArticle.setCollectionId(collectionId);
        collectionArticle.setArticleId(articleId);
        long count = collectionArticleService.count(new LambdaQueryWrapper<CollectionArticle>().eq(CollectionArticle::getCollectionId, collectionId));
        collectionArticle.setOrderNum((int) (count + 1));
        return AjaxResult.status(collectionArticleService.save(collectionArticle));
    }

    @GetMapping("/getCollectionArticleList")
    @ApiOperation(value = "获取合集文章列表")
    public AjaxResult<List<ArticleVO>> getCollectionArticleList(Integer collectionId) {
        List<ArticleVO> articleVoS = collectionService.getCollectionArticleList(collectionId);
        return AjaxResult.data(articleVoS);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除文章")
    @ApiImplicitParam(name = "id", value = "文章id", required = true)
    public AjaxResult<Boolean> delete(Integer id) {
        return AjaxResult.status(articleService.removeById(id));
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页查询文章列表")
    public AjaxResult<IPage<ArticleVO>> page(@RequestBody @Validated SearchArticleParam article) {
        IPage<ArticleVO> page = articleService.getArticlePage(article);
        return AjaxResult.data(page);
    }

    @GetMapping("/addArticleTag")
    @ApiOperation(value ="给指定文章新增文章标签", notes = "给指定文章新增文章标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleId", value = "文章id", required = true),
            @ApiImplicitParam(name = "tagId", value = "标签id", required = true)
    })
    public AjaxResult<Boolean> addArticleTag(Integer articleId, Integer tagId) {
        ArticleTag articleTag = new ArticleTag();
        articleTag.setArticleId(articleId);
        articleTag.setTagId(tagId);
        return AjaxResult.status(articleTagService.save(articleTag));
    }

    @GetMapping("/getTopArticle")
    @ApiOperation(value ="获取置顶文章", notes = "获取置顶文章")
    public AjaxResult<ArticleVO> getTopArticle() {
        return AjaxResult.data(articleService.getTopArticle());
    }

    @GetMapping("/setTopArticle")
    @ApiOperation(value ="设置置顶文章", notes = "设置置顶文章")
    @ApiImplicitParam(name = "articleId", value = "文章id", required = true)
    public AjaxResult<Boolean> setTopArticle(Integer articleId) {
        SysSetting setting = sysSettingService.getOne(new LambdaQueryWrapper<SysSetting>().eq(SysSetting::getCode,Constant.TOP_ARTICLE_CODE));
        if (StringUtils.isEmpty(setting)) {
            setting = new SysSetting();
            setting.setCode(Constant.TOP_ARTICLE_CODE);
            setting.setValue(String.valueOf(articleId));
            setting.setConfigName("置顶文章");
            setting.setRemark("置顶文章");
            sysSettingService.save(setting);
            return AjaxResult.success();
        }
        return AjaxResult.status(articleService.setTopArticle(articleId));
    }

}
